// index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

let app = getApp();

let api = require('../../utils/api.js');
let resultData = require('../../utils/data.js');


Page({
  data: {
    motto: 'Hello World',
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
    },
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
    bannerData : [],
    subJectData: [],
    swiperHeight:0
  },
  // getBannerData: function() {
  //     wx.request({
  //     url:'http://op.juhe.cn/onebox/movie/pmovie',
  //     data:{
  //       key: 'a8b932b7661aee98fcb3c89984626302',
  //       city: '广州'      
  //     },
  //     method:'get',
  //     success:function(res){
  //       console.log(1,res)
  //     }
  //   })
  // },
  // getBannerData(){
  //   api.getData('/onebox/movie/pmovie','get',{
  //     key:'a8b932b7661aee98fcb3c89984626302',
  //     city:'广州'
  //   }).then(res=>{
  //     console.log(res)
  //   },e=>{
  //     console.log(e)
  //   })
  // },
  getBannerData2(){
    api.getData('select_banner_list','get').then(res=>{
      this.setData({
        bannerData:res.data
      })
    },e=>{
      console.log(e)
    })
  },
  getSubjectData(){
    api.getData('get_movie_list','get').then(res=>{
      console.log(res.data)
      this.setData({
        subJectData:res.data
      })
    },e=>{
      console.log(e)
    })
  },
   imgLoad:function(e){
    //  console.log(e)
    var winWidth =  wx.getSystemInfoSync().windowWidth;
    var imgWidth = e.detail.width;
    var imgHeight = e.detail.height;
    this.setData({
      swiperHeight:winWidth * imgHeight/imgWidth + 'px'
    })
   },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.setData({
    //   bannerData:resultData.bannerData,
    //   subJectData:resultData.subJectData
    // })
    // this.getBannerData()
    this.getBannerData2()
    this.getSubjectData()
  },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    const { nickName } = this.data.userInfo
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onInputChange(e) {
    const nickName = e.detail.value
    const { avatarUrl } = this.data.userInfo
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
})
